package main

import "fmt"

// 数组和多维数组,以及参数传递数组
func main() {
	var balance = [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0}

	for e := range balance {
		fmt.Println(balance[e])
	}

	fmt.Println("========================")

	var a = [5][2]int{{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
	var i, j int

	/* 输出数组元素 */
	for i = 0; i < 5; i++ {
		for j = 0; j < 2; j++ {
			fmt.Printf("a[%d][%d] = %d\n", i, j, a[i][j])
		}
	}

	fmt.Println("========================")

	/* 数组长度为 5 */
	var numSum = []int{1000, 2, 3, 17, 50}
	var avg float32

	/* 数组作为参数传递给函数 */
	avg = getAverage(numSum, 5)

	/* 输出返回的平均值 */
	fmt.Printf("平均值为: %f ", avg)

	fmt.Println("========================")
	num := getMaxNum(numSum)
	fmt.Print(num)
}

func getMaxNum(arr []int) int {
	var max int
	max = arr[0]
	for e := range arr {
		if arr[e] >= max {
			max = arr[e]
		}
	}
	return max
}

// 计算平均值
func getAverage(arr []int, size int) float32 {
	var i, sum int
	var avg float32

	for i = 0; i < size; i++ {
		sum += arr[i]
	}

	avg = float32(sum) / float32(size)

	return avg
}
